//
//  PictureDetailViewController.m
//  Project_A Gary
//
//  Created by leilei on 16/3/31.
//  Copyright © 2016年 www.lei.com. All rights reserved.
//

#import "PictureDetailViewController.h"
#import "PictureDetailModel.h"
#import "PictureDetailCollectionViewCell.h"

@interface PictureDetailViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain)NSMutableArray *phototArr;
@property (nonatomic, retain)UICollectionView *collectionV;

@end

@implementation PictureDetailViewController

-(void)dealloc
{
    [_mId release];
    [_phototArr release];
    [super dealloc];
    
}






- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];

    [self createCollectionView];
    [self dataHandle];
    
    
}

#pragma mark - 创建collectionView
- (void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 0;
    flowLayout.itemSize = CGSizeMake(BOUNDS_WIDTH, 300);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 160, BOUNDS_WIDTH, 300) collectionViewLayout:flowLayout];
    self.collectionV.backgroundColor = [UIColor blackColor];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    self.collectionV.pagingEnabled = YES;
    [self.view addSubview:self.collectionV];
    
    [self.collectionV registerClass:[PictureDetailCollectionViewCell class] forCellWithReuseIdentifier:@"PictureDetailCollectionViewCell"];
    [_collectionV release];
    
}

#pragma mark - 数据解析
- (void)dataHandle
{
    NSString *str = [NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v2/photo/albums?modelId=%@", self.mId];
    [NetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        self.phototArr = [PictureDetailModel transformWithArray:responseObject[@"photos"]];
        
        [_collectionV reloadData];
    } filed:^(NSError *error) {
        
    }];
    
}


#pragma mark - collectionView必用的两个方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.phototArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PictureDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PictureDetailCollectionViewCell" forIndexPath:indexPath];
    PictureDetailModel *pcModel = self.phototArr[indexPath.item];
    cell.lab.text = pcModel.url;
    
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:pcModel.url]];
    
    
    
    return cell;
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
